package com.beidouin.sensortest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class SimpleChartView extends View {

    public int beat = 0;
    public  int size =0;
    private int Xstart=400;
    private int Ystart=50;//起始坐标
    private int Xlength=1300;
    private int Ylength=500;//XY轴长度
    public static List<Integer> data=new ArrayList<Integer>();//绘图数据
    public static List<Integer> datas=new ArrayList<Integer>();//源数据

    Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
    Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),R.drawable.heart2);

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            /*mHandler.postDelayed(new Runnable() {
                public void run() {
                    mHandler.sendMessage(mHandler.obtainMessage());
                    SimpleChartView.this.invalidate();//重绘
                }
            }, 20);*///自己更新
        }
    };

    public SimpleChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHandler.sendMessage(mHandler.obtainMessage());
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画笔
        Paint paint=new Paint();
        //画笔预设，抗锯齿
       // paint.setAntiAlias(true);
        /////////////////画心脏图片
       /* if(beat==0){
            canvas.drawBitmap(bitmap2,1100,500,paint);
        }else{
            canvas.drawBitmap(bitmap1,1100,500,paint);
            beat = beat-1;
        }*/
        //画Y轴
        canvas.drawLine(Xstart+1, Ystart+10, Xstart+1, Ystart+Ylength, paint);
        canvas.drawLine(Xstart+Xlength-100, Ystart+10, Xstart+Xlength-100, Ystart+Ylength, paint);///右侧Y轴
        //画X轴
        canvas.drawLine(Xstart, Ystart+Ylength, Xstart+Xlength+10, Ystart+Ylength, paint);
        canvas.drawLine(Xstart, Ystart+10, Xstart+Xlength+10, Ystart+10, paint);
        //定义刻度
        String[] str={"","","0"};
        //画刻度
        for (int i = 0; i < 5; i++) {
            canvas.drawLine(Xstart, Ystart+i*150, Xstart-5, Ystart+i*150, paint);
           // canvas.drawText(str[i], Xstart-65, Ystart+i*150+3, paint);
        }
        for (int i = 0; i < 14; i++) {
            canvas.drawLine(Xstart+i*100, Ystart+Ylength, Xstart+i*100, Ystart+Ylength-5, paint);
        }
        //画小箭头
       // canvas.drawLine(Xstart, Ystart-10, Xstart+3, Ystart-6, paint);
       // canvas.drawLine(Xstart, Ystart-10, Xstart-3, Ystart-6, paint);
        System.out.println("Data.size = " + data.size());
        //画折线,处理绘图数据
        size = datas.size();
        int max=0,min=1000000;
        int range;
        int k;
        for(int i = 0; i<size;i++){
            /*if(datas.get(i)<=96){
                if(i == 0)k=128;

                else {
                    int ii = i-1;
                    k = datas.get(ii);
                }
                datas.set(i,k);
            }*/
            if(max<datas.get(i))max = datas.get(i);
            if(min>datas.get(i))min = datas.get(i);
        }
        range = max-min;
        //range = max-96;
        if(range < 10){
            data.clear();
            for (int i = 0; i < size; i++) {
                int num;
                num =  datas.get(i);
                data.add(i, num);///写入绘图数据///每次都更新
            }
        } else {
            data.clear();
            for (int i = 0; i < size; i++) {
                int num;
                num = (int) ((double) (datas.get(i) -min) / (double) range * 300)+10;
                data.add(i, num);///写入绘图数据///每次都更新
            }
        }
        if(size>1){
            for (int i = 0; i < size-1; i++) {

                canvas.drawLine(Xstart+i*10, Ystart+Ylength-data.get(i)-30, Xstart+(i+1)*10, Ystart+Ylength-data.get(i+1)-30, paint);
            }
        }
        if(size>120)
            datas.clear();
    }
}
